package com.cyn.aframework.scriptenginemanager;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class BasicScripting {
    public void greet() throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("groovy");
        //ScriptEngine engine = manager.getEngineByExtension("js");  
        //ScriptEngine engine = manager.getEngineByMimeType("text/javascript");  
        if (engine == null) {  
            throw new RuntimeException("找不到JavaScript语言执行引擎。");  
        }
        String s = " def getString(name1,name2){return com.cyn.aframework.scriptenginemanager.TestClass.getString(name1,name2)};";
        engine.eval(s);

        Bindings bindings = engine.createBindings();
        bindings.put("name1", "test1");
        bindings.put("name2", "test2");

        Boolean b = (Boolean) engine.eval("getString(name1,name2) == '1'",bindings);
        System.out.println(b);

    }
      
    public static void main(String[] args) {  
        try {  
            new BasicScripting().greet();  
        } catch (ScriptException ex) {  
            ex.printStackTrace();
        }  
    }  
}  